package com.QSystem.service;

import com.QSystem.mapper.ChoiceMapper;
import com.QSystem.mapper.TitleMapper;
import com.QSystem.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TitleService {
    @Autowired
    TitleMapper titleMapper;
    @Autowired
    ChoiceService choiceService;
    @Autowired
    ChoiceMapper choiceMapper;
    
    //根据传来的surid去修改所有title的值
    //先根据surid删除所有的title，然后再增加传过来的title
    public void updateTitles(WholeSurvey wholeSurvey){
        Integer surId=wholeSurvey.getSurvey().getSurId();
        System.out.println("根据surId修改title值，suiId为:"+surId);
        titleMapper.deleteBysurid(surId);
        System.out.println("获得的wholetitles："+wholeSurvey.getWholeTitles());

        for (WholeTitle wholeTitle : wholeSurvey.getWholeTitles()) {
            wholeTitle.getTitle().setSurId(surId);
            System.out.println("插入的title为:"+wholeTitle.getTitle());
            titleMapper.addTitle(wholeTitle.getTitle());//添加单个title
            //要根据surid以及num去找
            Integer titId=titleMapper.queryByNum(wholeTitle.getTitle()).getTitId();
            //然后根据titid去更改相应的option，也是先删除，再添加

            int i=0;
            choiceMapper.delete(titId);
            for (Choice choice : wholeTitle.getChoices()) {
                System.out.println(i++);
                System.out.println(titId);
                choice.setTitId(titId);
                //System.out.println("插入的choice为:"+choice);
                choiceMapper.add(choice);//添加单个choice
            }

        }
    }

    //返回wholetitles
    WholeSurvey queryBySurid(WholeSurvey wholeSurvey){

        Integer surId=wholeSurvey.getSurvey().getSurId();//获取当前问卷的编号
        //根据surid编号获取所有的title
        List<Title> titles = titleMapper.queryBySurid(surId);
        for (Title title : titles) {
            WholeTitle wholeTitle=new WholeTitle();
            wholeTitle.setTitle(title);
//            choiceMapper.queryByTitid(title.getTitId());//这是某个titid所有的choice
            wholeTitle.setChoices(choiceMapper.queryByTitid(title.getTitId()));

            wholeSurvey.getWholeTitles().add(wholeTitle);
        }
        return wholeSurvey;
    }
}
